package com.hs.srm.admin.modules.pricemanagement.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.pricemanagement.dao.HistoricalPriceDao;
import com.hs.srm.admin.modules.pricemanagement.entity.HistoricalPriceEntity;
import com.hs.srm.admin.modules.pricemanagement.service.HistoricalPriceService;
import com.hs.srm.admin.modules.pricemanagement.vo.EssentialInformationVO;
import com.hs.srm.admin.modules.requisition.vo.SupplyGoodsEntityVO;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yanfei
 * @date 2019年3月1日
 * 询价单头信息
 */
@Service("historicalPrice")
public class HistoricalPriceServiceImpl extends ServiceImpl<HistoricalPriceDao, HistoricalPriceEntity> implements HistoricalPriceService {
    @Autowired
    private HistoricalPriceDao historicalPriceDao;

    @Override
    @DataFilter
    public PageUtils queryPage(Map<String, Object> params) {

        String vendorCode = (String) params.get("vendorCode");
        String vendorName = (String) params.get("vendorName");
        Page<HistoricalPriceEntity> page = this.selectPage(
                new QueryParam<HistoricalPriceEntity>(params).getPage(),
                new EntityWrapper<HistoricalPriceEntity>()
                        .like(StringUtils.isNotBlank(vendorName), "vendor_name", vendorName)
                        .eq(StringUtils.isNotBlank(vendorCode), "vendor_code", vendorCode));
        return new PageUtils(page);
    }

    @Override
    public List<HistoricalPriceEntity> findbyIds(List<String> id) {

        return this.baseMapper.findbyIds(id);
    }

    /**
     * 查找供应商数据，包括正式和临时供应商
     *
     * @param id
     * @return
     */
    @Override
    public List<HistoricalPriceEntity> findUnionVendorByIds(ArrayList<String> id) {
        return this.baseMapper.findUnionVendorByIds(id);
    }


    /**
     * 非生产性生成订单时把价格保存到历史订单中
     *
     * @param supplyGoodsEntity
     * @return
     */
    @Override
    public HistoricalPriceEntity convertToHistoryPrice(SupplyGoodsEntityVO supplyGoodsEntity) {
        HistoricalPriceEntity historicalPriceEntity = new HistoricalPriceEntity();
        historicalPriceEntity.setVendorCode(supplyGoodsEntity.getVendorCode());
        historicalPriceEntity.setVendorName(supplyGoodsEntity.getVendorName());
        historicalPriceEntity.setPriceUnit(supplyGoodsEntity.getPriceUnit());
        historicalPriceEntity.setTaxCode(supplyGoodsEntity.getTaxCode());
        historicalPriceEntity.setCreatedBy(supplyGoodsEntity.getCreatedBy());
        historicalPriceEntity.setRemark(supplyGoodsEntity.getRemark());
        historicalPriceEntity.setReplyDeliveryDate(supplyGoodsEntity.getDeliveryDate());
        historicalPriceEntity.setRequestCode(supplyGoodsEntity.getPurchRequestCode());
        historicalPriceEntity.setCurrency(supplyGoodsEntity.getCurrency());
        historicalPriceEntity.setWerks(supplyGoodsEntity.getFactory());
        historicalPriceEntity.setMaterialDesc(supplyGoodsEntity.getMaterialDesc());
        historicalPriceEntity.setPurchaseGrp(supplyGoodsEntity.getPurchGroup());
        historicalPriceEntity.setPurchaseOrg(supplyGoodsEntity.getPurchOrg());
        historicalPriceEntity.setItemNo(supplyGoodsEntity.getPurchRequestItemCode());
        historicalPriceEntity.setPrice(supplyGoodsEntity.getTaxPrice());
        historicalPriceEntity.setCreatedDate(new Date());
        Integer count = historicalPriceDao.getRecordCount(supplyGoodsEntity.getVendorCode(), supplyGoodsEntity.getMaterialDesc(), supplyGoodsEntity.getTaxPrice());
        if (count != 0) {
            return null;
        }
        return historicalPriceEntity;
    }

    /**
     * 生产性生成订单时把价格保存到历史订单中
     *
     * @param essentialInformationVO
     * @return
     */
    @Override
    public HistoricalPriceEntity convertToProHistoryPrice(EssentialInformationVO essentialInformationVO) {
        HistoricalPriceEntity historicalPriceEntity = new HistoricalPriceEntity();
        historicalPriceEntity.setVendorCode(essentialInformationVO.getSapCode());
        historicalPriceEntity.setVendorName(essentialInformationVO.getVendorName());
        historicalPriceEntity.setPriceUnit(essentialInformationVO.getPriceUnit());
        historicalPriceEntity.setTaxCode(essentialInformationVO.getTaxCode());
        historicalPriceEntity.setCreatedBy(essentialInformationVO.getCreatedBy());
        historicalPriceEntity.setRemark(essentialInformationVO.getRemark());
        historicalPriceEntity.setRequestCode(essentialInformationVO.getPurchRequestCode());
        historicalPriceEntity.setCurrency(essentialInformationVO.getCurrency());
        historicalPriceEntity.setWerks(essentialInformationVO.getFactory());
        historicalPriceEntity.setMaterialDesc(essentialInformationVO.getMaterialDesc());
        historicalPriceEntity.setPurchaseGrp(essentialInformationVO.getPurchGroup());
        historicalPriceEntity.setPurchaseOrg(essentialInformationVO.getPurchOrg());
        historicalPriceEntity.setItemNo(essentialInformationVO.getPurchRequestItemCode());
        historicalPriceEntity.setPrice(essentialInformationVO.getPrice());
        historicalPriceEntity.setCreatedDate(new Date());
        historicalPriceEntity.setMaterialCode(essentialInformationVO.getMaterialCode());

        Integer count = historicalPriceDao.getProRecordCount(essentialInformationVO.getSapCode(), essentialInformationVO.getMaterialCode(), essentialInformationVO.getPrice());
        if (count != 0) {
            return null;
        }
        return historicalPriceEntity;
    }


}
